################################################################################
################################################################################
####################### ANALYSIS SCRIPT 2 ######################################
############## STRUCTURAL DIFFERENCE IN ISSUE EMPHASIS #########################
################################################################################
################################################################################

##### ISSUE ANALYSES ######
load("Data/EP_debates_07062023.Rdata")
library(lme4)
library(texreg)
library(sjPlot)
library(sjstats)
library(fixest)
library(ggplot2)
library(ggeffects)


### BEGIN BY CENTERING AND SCALING EVERYTHING

EP_debates$radical <- abs(EP_debates$lrgen - 5)
range(EP_debates$radical, na.rm = TRUE)

centFUN <- function(x) {
  x - mean(x, na.rm = TRUE)
}

result <- apply(EP_debates[,c(22:24, 27:71, 73:75, 77:107,117:118)], MARGIN = 2, FUN = centFUN)
result <- as.data.frame(result)


EP_eurobar_cent <- cbind(EP_debates[,-c(22:24, 27:71, 73:75, 77:107,117:118)], result)

rm(list=setdiff(ls(), "EP_eurobar_cent"))

EP_eurobar_cent$embed_dict_scale <- scale(EP_eurobar_cent$embed_dict)
EP_eurobar_cent$aut_dict_scale <- scale(EP_eurobar_cent$aut_dict)
EP_eurobar_cent$immi_dict_scale <- scale(EP_eurobar_cent$immi_dict)
EP_eurobar_cent$int_dict_scale <- scale(EP_eurobar_cent$int_dict)
EP_eurobar_cent$lrgen_scale <- scale(EP_eurobar_cent$lrgen)
EP_eurobar_cent$radical_scale <- scale(EP_eurobar_cent$radical)

EP_eurobar_cent$aut_sal <- scale(EP_eurobar_cent$mip_n_govdebt_ipol)
EP_eurobar_cent$immi_sal <- scale(EP_eurobar_cent$mip_n_immigration_ipol)
EP_eurobar_cent$eum_ipol <- scale(EP_eurobar_cent$eum_ipol)
EP_eurobar_cent$image_ipol <- scale(EP_eurobar_cent$image_ipol)


########### STRUCTURAL DIFFERENCES  MAIN MODELS PAPER ##########################

AUT_random_CON <- lmer(aut_dict_scale ~ challenge + lrgen_scale  + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_random_CON)


AUT_random_Opp <- lmer(aut_dict_scale ~ challenge + lrgen_scale +EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_random_Opp)


AUT_random_Group <- lmer(aut_dict_scale ~ challenge + lrgen_scale + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 +EU_party + as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_random_Group)


texreg(list(AUT_random_CON, AUT_random_Opp, AUT_random_Group),
       file = "Tables/austerity_diffs_01032024.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              'nat_opp' = "Nat. Opposition",
                              "challenge:left" = "Challenger * Left",
                              "challenge:right" = "Challenger * Right"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c( "Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c( "Yes", "Yes", "Yes"),
                              "EP-Group Fixed Effects" = c( "No", "No", "Yes")),
       custom.model.names = c("Austerity", "Austerity", "Austerity"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "Structural Differences between Challenger and Dominant Parties")




######## IMMIGRATION


Immi_random_CON <- lmer(immi_dict_scale ~ challenge  +  lrgen_scale  + de_macro + de_civil + de_health + 
                          de_agri + de_labour + de_edu + de_envi + de_energy + 
                          de_immi + de_transport + de_law + de_welfare  + de_commerce +
                          de_defense + de_techno + de_trade + de_intern + de_govern +
                          de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                          (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_random_CON)

Immi_random_CON_Opp <- lmer(immi_dict_scale ~ challenge  +  lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                          de_agri + de_labour + de_edu + de_envi + de_energy + 
                          de_immi + de_transport + de_law + de_welfare  + de_commerce +
                          de_defense + de_techno + de_trade + de_intern + de_govern +
                          de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                          (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_random_CON_Opp)


Immi_random_CON_Group <- lmer(immi_dict_scale ~ challenge  +  lrgen_scale   + de_macro + de_civil + de_health + 
                              de_agri + de_labour + de_edu + de_envi + de_energy + 
                              de_immi + de_transport + de_law + de_welfare  + de_commerce +
                              de_defense + de_techno + de_trade + de_intern + de_govern +
                              de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + as.factor(country) +
                              (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_random_CON_Group)



texreg(list(Immi_random_CON, Immi_random_CON_Opp, Immi_random_CON_Group),
       file = "Tables/immigration_diffs_01032024.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition",
                              "challenge:left" = "Challenger * Left",
                              "challenge:right" = "Challenger * Right"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c( "Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c( "Yes", "Yes", "Yes"),
                              "EP-Group Fixed Effects" = c( "No", "No", "Yes")),
       custom.model.names = c("Immigration", "Immigration", "Immigration"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "Structural Differences between Challenger and Dominant Parties")


####### EU-INTEGRATION #######

Int_random_CON <- lmer(int_dict_scale ~ challenge  +  lrgen_scale  + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Int_random_CON)


Int_random_CON_Opp <- lmer(int_dict_scale ~ challenge  +  lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Int_random_CON_Opp)

Int_random_CON_Group <- lmer(int_dict_scale ~ challenge  +  lrgen_scale   + de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + EU_party + as.factor(year) + as.factor(country) +
                             (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Int_random_CON_Group)


texreg(list(Int_random_CON, Int_random_CON_Opp, Int_random_CON_Group),
       file = "Tables/int_diffs_01032024.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition",
                              "challenge:left" = "Challenger * Left",
                              "challenge:right" = "Challenger * Right"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c( "Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c( "Yes", "Yes", "Yes"),
                              "EP-Group Fixed Effects" = c( "No", "No", "Yes")),
       custom.model.names = c("EU-Integration", "EU-Integration", "EU-Integration"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "Structural Differences between Challenger and Dominant Parties")


####### Polynomial models #######



EP_eurobar_cent$chall_fact <- as.factor(ifelse(EP_eurobar_cent$challenge == 1, "Challenger", "Mainstream"))



poly_lr_model_immi <- lmer(immi_dict_scale ~ challenge*I(lrgen^2) + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + country +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(poly_lr_model_immi)

poly_lr_model_aut <- lmer(aut_dict_scale ~ challenge*I(lrgen^2) + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + country +
                             (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(poly_lr_model_aut)

poly_lr_model_int <- lmer(int_dict_scale ~ challenge*I(lrgen^2) + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + country +
                             (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(poly_lr_model_int)

texreg(list(poly_lr_model_immi, poly_lr_model_aut, poly_lr_model_int),
       file = "Tables/issues_poly_model_01032024.tex",
       include.ci = FALSE,
       omit.coef = "(de)|(country)|(EU)|(year)|(proced)",
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "EP-Group Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       label = "Table1",
       caption = "Interaction of Party Ideology and Party Type (incl. polynomial)")



plot_poly_lr_model_immi <- lmer(immi_dict_scale ~ chall_fact*I(lrgen^2) + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + country +
                             (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

plot_poly_lr_model_aut <- lmer(aut_dict_scale ~ chall_fact*I(lrgen^2) + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                            de_agri + de_labour + de_edu + de_envi + de_energy + 
                            de_immi + de_transport + de_law + de_welfare  + de_commerce +
                            de_defense + de_techno + de_trade + de_intern + de_govern +
                            de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + country +
                            (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

plot_poly_lr_model_int <- lmer(int_dict_scale ~ chall_fact*I(lrgen^2) + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                            de_agri + de_labour + de_edu + de_envi + de_energy + 
                            de_immi + de_transport + de_law + de_welfare  + de_commerce +
                            de_defense + de_techno + de_trade + de_intern + de_govern +
                            de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + country +
                            (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))




poly_lr_plot_immi <- plot_model(plot_poly_lr_model_immi, type = "pred",
                           terms = c("lrgen[-5,-4,-3,-2,-1,0, 1, 2, 3 , 4, 5]", "chall_fact"),
                           title = "",
                           axis.title = c("Ideology (Left to Right) - CHES", "Emphasis of Immigration \n (standardized and centered around 0)"),
                           colors = "bw",
                           legend.title = "Party",
                           ci.lvl = .95, rg.limit = 74844) +
  theme(text = element_text(size = 16))+
  theme_classic()+
  theme(legend.position = "none")

poly_lr_plot_aut <- plot_model(plot_poly_lr_model_aut, type = "pred",
                                terms = c("lrgen[-5,-4,-3,-2,-1,0, 1, 2, 3 , 4, 5]", "chall_fact"),
                                title = "Marginal Effects of Party Ideology",
                                axis.title = c("Ideology (Left to Right) - CHES", "Emphasis of Austerity \n (standardized and centered around 0)"),
                                colors = "bw",
                                legend.title = "Party",
                                ci.lvl = .95, rg.limit = 74844) +
  theme(text = element_text(size = 16))+
  theme_classic()+
  theme(legend.position = "none")


poly_lr_plot_int <- plot_model(plot_poly_lr_model_aut, type = "pred",
                               terms = c("lrgen[-5,-4,-3,-2,-1,0, 1, 2, 3 , 4, 5]", "chall_fact"),
                               title = "",
                               axis.title = c("Ideology (Left to Right) - CHES", "Emphasis of EU-Integration \n (standardized and centered around 0)"),
                               colors = "bw",
                               legend.title = "Party",
                               ci.lvl = .95, rg.limit = 74844) +
  theme(text = element_text(size = 16))+
  theme_classic()

issue_poly_plots <- poly_lr_plot_immi + poly_lr_plot_aut + poly_lr_plot_int
issue_poly_plots

ggsave("Figures/poly_issue_plots_01032024.png",issue_poly_plots, width = 280, device = "png", height = 100, units = "mm", dpi=300, scale = 1.2)


##### STRUCTURAL DIFFERENCES FIXED EFFECTS MODELS APPENDIX #######################3
AUT_fixed_CON <- feols(aut_dict_scale ~ challenge  + lrgen_scale + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5| year + country , data = EP_eurobar_cent, cluster = "party_id")
summary(AUT_fixed_CON)
Immi_fixed_CON <- feols(immi_dict_scale ~ challenge + lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                          de_agri + de_labour + de_edu + de_envi + de_energy + 
                          de_immi + de_transport + de_law + de_welfare  + de_commerce +
                          de_defense + de_techno + de_trade + de_intern + de_govern +
                          de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5| year + country , data = EP_eurobar_cent, cluster = "party_id")
summary(Immi_fixed_CON)
Int_fixed_CON <- feols(int_dict_scale ~ challenge + lrgen_scale  + EP_Coa +  nat_opp + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 | year + country, data = EP_eurobar_cent, cluster = "party_id")
summary(Int_fixed_CON)


texreg(list(AUT_fixed_CON, Immi_fixed_CON, Int_fixed_CON),
       tex=TRUE, file ='Tables/Issues_fixed_080623.tex',
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05, 0.1),
       custom.coef.map = list("challenge" = "Challenger",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.model.names = c("Austerity", "Immigration", "European Integration"),
       custom.note = "%stars. Clustered Standard Errors (National Party) in Parentheses",
       digits = 2,
       caption = "Structural Differences between Challenger and Dominant Parties")


##### ALT MODEL SPECS STRUCTURAL DIFFERENCES  APPENDIX #############


AUT_random_DR <- lmer(aut_dict_scale ~ challenge*right + challenge*left + EP_Coa + nat_opp + as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AUT_random_DR)

AUT_randomslope <- lmer(aut_dict_scale ~ challenge*right + challenge*left  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                          de_agri + de_labour + de_edu + de_envi + de_energy + 
                          de_immi + de_transport + de_law + de_welfare  + de_commerce +
                          de_defense + de_techno + de_trade + de_intern + de_govern +
                          de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                          (1|mep_ids) + (challenge|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(AUT_randomslope)


Immi_random_DR <- lmer(immi_dict_scale ~ challenge*right + challenge*left + EP_Coa + nat_opp + as.factor(year) + as.factor(country) +
                        (1|mep_ids) + (1|party_id) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_random_DR)


Immi_randomslope <- lmer(immi_dict_scale ~ challenge*right + challenge*left + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                          de_agri + de_labour + de_edu + de_envi + de_energy + 
                          de_immi + de_transport + de_law + de_welfare  + de_commerce +
                          de_defense + de_techno + de_trade + de_intern + de_govern +
                          de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                          (1|mep_ids) + (challenge|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Immi_randomslope)


Int_random_CON <- lmer(int_dict_scale ~ challenge*right + challenge*left  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Int_random_CON)



Int_random_DR <- lmer(int_dict_scale ~ challenge*right + challenge*left + EP_Coa + nat_opp + as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (1|party_id) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Int_random_DR)


Int_randomslope <- lmer(int_dict_scale ~ challenge*right + challenge*left  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                         de_agri + de_labour + de_edu + de_envi + de_energy + 
                         de_immi + de_transport + de_law + de_welfare  + de_commerce +
                         de_defense + de_techno + de_trade + de_intern + de_govern +
                         de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                         (1|mep_ids) + (challenge|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(Int_randomslope)



texreg(list(AUT_randomslope, AUT_random_DR, Immi_randomslope, Immi_random_DR, Int_randomslope, Int_random_DR),
       file = "Tables/Issues_alt_specs_080623.tex",
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05, 0.1),
       custom.coef.map = list("challenge" = "Challenger",
                              "challenge:right" = "Challenger * Right",
                              "challenge:left" = "Challenger * Left",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "No", "Yes", "No", "Yes", "No"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                              "Random Slopes" = c("Yes", "No", "Yes", "No", "Yes", "No"),
                              "Random Intercepts (Debates)" = c("No", "Yes", "No", "Yes", "No", "Yes")),
       custom.model.names = c("Austerity", "Austerity", "Immigration", "Immigration", "European Integration", "European Integration"),
       digits = 2,
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       caption = "Structural Differences between Challenger and Dominant Parties")



###### PARTY FIXED EFFECTS MODELS


AUT_random_CON_PF <- lmer(aut_dict_scale ~ dominant  +  lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                           de_agri + de_labour + de_edu + de_envi + de_energy + 
                           de_immi + de_transport + de_law + de_welfare  + de_commerce +
                           de_defense + de_techno + de_trade + de_intern + de_govern +
                           de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(party_id) +
                           (1|mep_ids), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(AUT_random_CON_PF)


Immi_random_CON_PF <- lmer(immi_dict_scale ~ dominant  +  lrgen_scale  +  EP_Coa + nat_opp +  de_macro + de_civil + de_health + 
                            de_agri + de_labour + de_edu + de_envi + de_energy + 
                            de_immi + de_transport + de_law + de_welfare  + de_commerce +
                            de_defense + de_techno + de_trade + de_intern + de_govern +
                            de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(party_id) +
                            (1|mep_ids), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(Immi_random_CON_PF)


Int_random_CON_PF <- lmer(int_dict_scale ~ dominant  +  lrgen_scale  +  EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                            de_agri + de_labour + de_edu + de_envi + de_energy + 
                            de_immi + de_transport + de_law + de_welfare  + de_commerce +
                            de_defense + de_techno + de_trade + de_intern + de_govern +
                            de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(party_id) +
                            (1|mep_ids), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(Int_random_CON_PF)



texreg(list(AUT_random_CON_PF, Immi_random_CON_PF, Int_random_CON_PF),
       file = "Tables/issues_partyfix_06082023.tex",
       include.ci = FALSE,
       custom.coef.map = list("dominant" = "Dominant Party",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Party Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.model.names = c("Austerity", "Immigration", "EU-Integration"),
       custom.note = "%stars. Random Intercepts for MEPs",
       digits = 2,
       caption = "Within-Party Differences between Challenger and Dominant Status")





##### SCRIPT RUN AND CHECKED ON 08.06.2023 ######
